package com.scau.ums.controller;

import com.scau.ums.mapper.CustomedNewsMapper;
import com.scau.ums.model.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

//发布新闻
@Controller
//@RequestMapping("publishNews")
public class PublishNewsController {
    private String errorPage = "/WEB-INF/jsp/authorityError.jsp";

    @Autowired
    public CustomedNewsMapper mapper;


    //    @RequestMapping("/publishNews")
//    public String index(ModelMap map, News schoolnews, HttpServletRequest request){
//
//
//        Date date=new Date();
//
//        News anews=new News();
//
//        //anews.set("你好");
//
//        String title=request.getParameter("title");
//        String words=request.getParameter("words");
//        String content=request.getParameter("editorValue");
//        //mapper.insert(anews);
////        if(content != null) {
////            //将内容设置进属性
////            request.setAttribute("content", content);
////        }
//        anews.setId(2);
//        anews.setTitle(title);
//        anews.setDate(date);
//        anews.setContent(content);
//        mapper.InsertNews(anews);
//        //mapper
//
//        return "publishnews";//返回WEB-INF/jsp/publishnews.jsp
//    }
    @RequestMapping(value = "/publishNews", method = RequestMethod.GET)
    public String topublishnews() {
        return "publishnews";
    }


    @RequestMapping(value = "/publishNews", method = RequestMethod.POST)
    public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Date date=new Date();

        News anews=new News();

       try{
           String title=request.getParameter("title");
           String words=request.getParameter("words");
//        String content=request.getParameter("content");
           String content=request.getParameter("editorValue");
//        anews.setId(2);
           anews.setTitle(title);
           anews.setDate(date);
           anews.setContent(content);
           mapper.InsertNews(anews);
           //mapper
       }catch (Exception e){
           e.printStackTrace();
           request.setAttribute("msg", "新闻发布失败，请重试！");
           request.setAttribute("redirect","/publishNews");
           request.getRequestDispatcher(errorPage).forward(request,response);
       }
        request.setAttribute("msg", "新闻发布成功！");
        request.setAttribute("redirect","/publishNews");
        request.getRequestDispatcher(errorPage).forward(request,response);

        return "publishnews";//返回WEB-INF/jsp/publishnews.jsp
    }



}